﻿// =================================================================== 
// 项目说明
//====================================================================
// SunCity@Copy Right 2006-2008
// 文件： Product.cs
// 项目名称：SunCity_MS
// 创建时间：2015/3/18
// 负责人：Chris
// ===================================================================
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using EMS20.Entity;
using EMS20.IDataAccessLayer;

namespace EMS20.DataAccessLayer
{
    /// <summary>
    /// 数据层实例化接口类 dbo.Product.
    /// </summary>
    public partial class ProductDataAccessLayer : IProductDataAccessLayer
    {
		#region 构造函数
		/// <summary>
		/// ??????
		/// </summary>
		public ProductDataAccessLayer()
		{
		}
		#endregion

        #region -----------实例化接口函数-----------
		
		#region 添加更新删除
		/// <summary>
		/// 向数据库中插入一条新记录。
		/// </summary>
		/// <param name="_ProductModel">Product实体</param>
		/// <returns>新插入记录的编号</returns>
		public int Create_ProductInsert(ProductEntity _ProductModel)
		{
			string sqlStr="CreateUpdateDelete_ProductEntity";
			int res;
			SqlParameter[] _param={
			new SqlParameter("@DataAction",SqlDbType.Int),
			new SqlParameter("@p_id",SqlDbType.Int),
			new SqlParameter("@p_photo_no",SqlDbType.VarChar),
			new SqlParameter("@p_title",SqlDbType.VarChar),
			new SqlParameter("@category_id",SqlDbType.Int),
			new SqlParameter("@img_url",SqlDbType.VarChar),
			new SqlParameter("@p_content",SqlDbType.VarChar),
			new SqlParameter("@sort_id",SqlDbType.Int),
			new SqlParameter("@click",SqlDbType.Int),
			new SqlParameter("@add_time",SqlDbType.DateTime),
			new SqlParameter("@is_lock",SqlDbType.TinyInt)
			};
			_param[0].Value=0;
			_param[1].Value=_ProductModel.p_id;
			_param[2].Value=_ProductModel.p_photo_no;
			_param[3].Value=_ProductModel.p_title;
			_param[4].Value=_ProductModel.category_id;
			_param[5].Value=_ProductModel.img_url;
			_param[6].Value=_ProductModel.p_content;
			_param[7].Value=_ProductModel.sort_id;
			_param[8].Value=_ProductModel.click;
			_param[9].Value=_ProductModel.add_time;
			_param[10].Value=_ProductModel.is_lock;
			res = (int)SqlHelper.ExecuteScalar(Conn.SqlConn,CommandType.StoredProcedure,sqlStr,_param);
			return res;
		}
		/// <summary>
		/// 向数据库中插入一条新记录。带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
		/// <param name="_ProductModel">Product实体</param>
		/// <returns>新插入记录的编号</returns>
		public int Create_ProductInsert(SqlTransaction sp,ProductEntity _ProductModel)
		{
			string sqlStr="CreateUpdateDelete_ProductEntity";
			int res;
			SqlParameter[] _param={
			new SqlParameter("@DataAction",SqlDbType.Int),
			new SqlParameter("@p_id",SqlDbType.Int),
			new SqlParameter("@p_photo_no",SqlDbType.VarChar),
			new SqlParameter("@p_title",SqlDbType.VarChar),
			new SqlParameter("@category_id",SqlDbType.Int),
			new SqlParameter("@img_url",SqlDbType.VarChar),
			new SqlParameter("@p_content",SqlDbType.VarChar),
			new SqlParameter("@sort_id",SqlDbType.Int),
			new SqlParameter("@click",SqlDbType.Int),
			new SqlParameter("@add_time",SqlDbType.DateTime),
			new SqlParameter("@is_lock",SqlDbType.TinyInt)
			};
			_param[0].Value=0;
			_param[1].Value=_ProductModel.p_id;
			_param[2].Value=_ProductModel.p_photo_no;
			_param[3].Value=_ProductModel.p_title;
			_param[4].Value=_ProductModel.category_id;
			_param[5].Value=_ProductModel.img_url;
			_param[6].Value=_ProductModel.p_content;
			_param[7].Value=_ProductModel.sort_id;
			_param[8].Value=_ProductModel.click;
			_param[9].Value=_ProductModel.add_time;
			_param[10].Value=_ProductModel.is_lock;
			res = (int)SqlHelper.ExecuteScalar(sp,CommandType.StoredProcedure,sqlStr,_param);
			return res;
		}


		/// <summary>
		/// 向数据表Product更新一条记录。
		/// </summary>
		/// <param name="_ProductModel">_ProductModel</param>
		/// <returns>影响的行数</returns>
		public int Create_ProductUpdate(ProductEntity _ProductModel)
		{
            string sqlStr="CreateUpdateDelete_ProductEntity";
			SqlParameter[] _param={
				new SqlParameter("@DataAction",SqlDbType.Int),
				new SqlParameter("@p_id",SqlDbType.Int),
				new SqlParameter("@p_photo_no",SqlDbType.VarChar),
				new SqlParameter("@p_title",SqlDbType.VarChar),
				new SqlParameter("@category_id",SqlDbType.Int),
				new SqlParameter("@img_url",SqlDbType.VarChar),
				new SqlParameter("@p_content",SqlDbType.VarChar),
				new SqlParameter("@sort_id",SqlDbType.Int),
				new SqlParameter("@click",SqlDbType.Int),
				new SqlParameter("@add_time",SqlDbType.DateTime),
				new SqlParameter("@is_lock",SqlDbType.TinyInt)
				};
				_param[0].Value=1;
				_param[1].Value=_ProductModel.p_id;
				_param[2].Value=_ProductModel.p_photo_no;
				_param[3].Value=_ProductModel.p_title;
				_param[4].Value=_ProductModel.category_id;
				_param[5].Value=_ProductModel.img_url;
				_param[6].Value=_ProductModel.p_content;
				_param[7].Value=_ProductModel.sort_id;
				_param[8].Value=_ProductModel.click;
				_param[9].Value=_ProductModel.add_time;
				_param[10].Value=_ProductModel.is_lock;
			return SqlHelper.ExecuteNonQuery(Conn.SqlConn,CommandType.StoredProcedure,sqlStr,_param);
		}
		/// <summary>
		/// 向数据表Product更新一条记录。带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
		/// <param name="_ProductModel">_ProductModel</param>
		/// <returns>影响的行数</returns>
		public int Create_ProductUpdate(SqlTransaction sp,ProductEntity _ProductModel)
		{
            string sqlStr="CreateUpdateDelete_ProductEntity";
			SqlParameter[] _param={
				new SqlParameter("@DataAction",SqlDbType.Int),
				new SqlParameter("@p_id",SqlDbType.Int),
				new SqlParameter("@p_photo_no",SqlDbType.VarChar),
				new SqlParameter("@p_title",SqlDbType.VarChar),
				new SqlParameter("@category_id",SqlDbType.Int),
				new SqlParameter("@img_url",SqlDbType.VarChar),
				new SqlParameter("@p_content",SqlDbType.VarChar),
				new SqlParameter("@sort_id",SqlDbType.Int),
				new SqlParameter("@click",SqlDbType.Int),
				new SqlParameter("@add_time",SqlDbType.DateTime),
				new SqlParameter("@is_lock",SqlDbType.TinyInt)
				};
				_param[0].Value=1;
				_param[1].Value=_ProductModel.p_id;
				_param[2].Value=_ProductModel.p_photo_no;
				_param[3].Value=_ProductModel.p_title;
				_param[4].Value=_ProductModel.category_id;
				_param[5].Value=_ProductModel.img_url;
				_param[6].Value=_ProductModel.p_content;
				_param[7].Value=_ProductModel.sort_id;
				_param[8].Value=_ProductModel.click;
				_param[9].Value=_ProductModel.add_time;
				_param[10].Value=_ProductModel.is_lock;
			return SqlHelper.ExecuteNonQuery(sp,CommandType.StoredProcedure,sqlStr,_param);
		}
		
		/// <summary>
		/// 删除数据表Product中的一条记录
		/// </summary>
	    /// <param name="p_id">p_id</param>
		/// <returns>影响的行数</returns>
		public int Create_ProductDelete(int p_id)
		{
        ProductEntity _ProductModel =new ProductEntity();
			string sqlStr="CreateUpdateDelete_ProductEntity";
			SqlParameter[] _param={
			new SqlParameter("@DataAction",SqlDbType.VarChar),
			new SqlParameter("@p_id",SqlDbType.Int),			
			new SqlParameter("@p_photo_no",SqlDbType.VarChar),
			new SqlParameter("@p_title",SqlDbType.VarChar),
			new SqlParameter("@category_id",SqlDbType.Int),
			new SqlParameter("@img_url",SqlDbType.VarChar),
			new SqlParameter("@p_content",SqlDbType.VarChar),
			new SqlParameter("@sort_id",SqlDbType.Int),
			new SqlParameter("@click",SqlDbType.Int),
			new SqlParameter("@add_time",SqlDbType.DateTime),
			new SqlParameter("@is_lock",SqlDbType.TinyInt)
			};
			_param[0].Value=2;
			_param[1].Value=p_id;
			_param[2].Value=_ProductModel.p_photo_no;
			_param[3].Value=_ProductModel.p_title;
			_param[4].Value=_ProductModel.category_id;
			_param[5].Value=_ProductModel.img_url;
			_param[6].Value=_ProductModel.p_content;
			_param[7].Value=_ProductModel.sort_id;
			_param[8].Value=_ProductModel.click;
			_param[9].Value=_ProductModel.add_time;
			_param[10].Value=_ProductModel.is_lock;
			return SqlHelper.ExecuteNonQuery(Conn.SqlConn,CommandType.StoredProcedure,sqlStr,_param);
		}
		
		/// <summary>
		/// 删除数据表Product中的一条记录,带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
	    /// <param name="p_id">p_id</param>
		/// <returns>影响的行数</returns>
		public int Create_ProductDelete(SqlTransaction sp,int p_id)
		{
         ProductEntity _ProductModel =new ProductEntity();
			string sqlStr="CreateUpdateDelete_ProductEntity";
			SqlParameter[] _param={
			new SqlParameter("@DataAction",SqlDbType.Int),
			new SqlParameter("@p_id",SqlDbType.Int),
			new SqlParameter("@p_photo_no",SqlDbType.VarChar),
			new SqlParameter("@p_title",SqlDbType.VarChar),
			new SqlParameter("@category_id",SqlDbType.Int),
			new SqlParameter("@img_url",SqlDbType.VarChar),
			new SqlParameter("@p_content",SqlDbType.VarChar),
			new SqlParameter("@sort_id",SqlDbType.Int),
			new SqlParameter("@click",SqlDbType.Int),
			new SqlParameter("@add_time",SqlDbType.DateTime),
			new SqlParameter("@is_lock",SqlDbType.TinyInt)
			};
			_param[0].Value=2;
			_param[1].Value=p_id;
			_param[2].Value=_ProductModel.p_photo_no;
			_param[3].Value=_ProductModel.p_title;
			_param[4].Value=_ProductModel.category_id;
			_param[5].Value=_ProductModel.img_url;
			_param[6].Value=_ProductModel.p_content;
			_param[7].Value=_ProductModel.sort_id;
			_param[8].Value=_ProductModel.click;
			_param[9].Value=_ProductModel.add_time;
			_param[10].Value=_ProductModel.is_lock;
			return SqlHelper.ExecuteNonQuery(sp,CommandType.StoredProcedure,sqlStr,_param);
		}
		#endregion
		
		#region 数据实体
		/// <summary>
		/// 得到  product 数据实体
		/// </summary>
		/// <param name="row">row</param>
		/// <returns>product 数据实体</returns>
		public ProductEntity Populate_ProductEntity_FromDr(DataRow row)
		{
			ProductEntity Obj = new ProductEntity();
			if(row!=null)
			{
				Obj.p_id = (( row["p_id"])==DBNull.Value)?0:Convert.ToInt32( row["p_id"]);
				Obj.p_photo_no =  row["p_photo_no"].ToString();
				Obj.p_title =  row["p_title"].ToString();
				Obj.category_id = (( row["category_id"])==DBNull.Value)?0:Convert.ToInt32( row["category_id"]);
				Obj.img_url =  row["img_url"].ToString();
				Obj.p_content =  row["p_content"].ToString();
				Obj.sort_id = (( row["sort_id"])==DBNull.Value)?0:Convert.ToInt32( row["sort_id"]);
				Obj.click = (( row["click"])==DBNull.Value)?0:Convert.ToInt32( row["click"]);
				Obj.add_time = (( row["add_time"])==DBNull.Value)?Convert.ToDateTime(1900-1-1):Convert.ToDateTime( row["add_time"]);
				Obj.is_lock = (( row["is_lock"])==DBNull.Value)?Convert.ToByte(0):Convert.ToByte( row["is_lock"]);
			}
			else
			{
				return null;
			}
			return Obj;
		}
		
        /// <summary>
		/// 得到  product 数据实体
		/// </summary>
		/// <param name="dr">dr</param>
		/// <returns>product 数据实体</returns>
		public ProductEntity Populate_ProductEntity_FromDr(IDataReader dr)
		{
			ProductEntity Obj = new ProductEntity();
			
				Obj.p_id = (( dr["p_id"])==DBNull.Value)?0:Convert.ToInt32( dr["p_id"]);
				Obj.p_photo_no =  dr["p_photo_no"].ToString();
				Obj.p_title =  dr["p_title"].ToString();
				Obj.category_id = (( dr["category_id"])==DBNull.Value)?0:Convert.ToInt32( dr["category_id"]);
				Obj.img_url =  dr["img_url"].ToString();
				Obj.p_content =  dr["p_content"].ToString();
				Obj.sort_id = (( dr["sort_id"])==DBNull.Value)?0:Convert.ToInt32( dr["sort_id"]);
				Obj.click = (( dr["click"])==DBNull.Value)?0:Convert.ToInt32( dr["click"]);
				Obj.add_time = (( dr["add_time"])==DBNull.Value)?Convert.ToDateTime(1900-1-1):Convert.ToDateTime( dr["add_time"]);
				Obj.is_lock = (( dr["is_lock"])==DBNull.Value)?Convert.ToByte(0):Convert.ToByte( dr["is_lock"]);
			
			return Obj;
		}
		#endregion
		
		/// <summary>
		/// 根据ID,返回一个Product对象
		/// </summary>
		/// <param name="p_id">p_id</param>
		/// <returns>Product对象</returns>
		public ProductEntity Get_ProductEntity (int p_id)
		{
			ProductEntity _obj=null;
			SqlParameter[] _param={
			new SqlParameter("@p_id",SqlDbType.Int)
			};
			_param[0].Value=p_id;
			string sqlStr="select * from Product where p_id=@p_id";
			using(SqlDataReader dr=SqlHelper.ExecuteReader(Conn.SqlConn,CommandType.Text,sqlStr,_param))
			{
				while(dr.Read())
				{
            		_obj=Populate_ProductEntity_FromDr(dr);
				}
			}
			return _obj;
		}
		/// <summary>
		/// 得到数据表Product所有记录
		/// </summary>
		/// <returns>数据集</returns>
		public IList< ProductEntity> Get_ProductAll()
		{
			IList< ProductEntity> Obj=new List< ProductEntity>();
			string sqlStr="select * from Product";
			using(SqlDataReader dr=SqlHelper.ExecuteReader(Conn.SqlConn,CommandType.Text,sqlStr))
			{
				while(dr.Read())
				{
					Obj.Add(Populate_ProductEntity_FromDr(dr));
				}
			}
			return Obj;
		}
		/// <summary>
        /// 检测是否存在根据主键
        /// </summary>
        /// <param name="p_id">p_id</param>
        /// <returns>是/否</returns>
		public bool IsExistProduct(int p_id)
		{
			SqlParameter[] _param={
                                      new SqlParameter("@p_id",SqlDbType.Int)
                                  };
            _param[0].Value=p_id;
            string sqlStr="select Count(*) from Product where p_id=@p_id";
            int a=Convert.ToInt32(SqlHelper.ExecuteScalar(Conn.SqlConn,CommandType.Text,sqlStr,_param));
            if(a>0)
            {
                return true;
            }
            else
            {
                return false;
            }
		}

        #endregion
		
		#region----------自定义实例化接口函数----------
		#endregion
    }
}
